﻿using Omu.ValueInjecter;
using System.Collections.Generic;

namespace Fx.Mvc
{
    public abstract class ViewModel<TEntity> : ViewModel<TEntity, int>
        where TEntity : class, new()
    {

    }

    public abstract class ViewModel<TEntity, TId> : Entity<TId>
        where TEntity : class, new()
    {
        public virtual void FromEntity(TEntity entity)
        {
            this.InjectFrom(entity);
        }

        public virtual TEntity ToEntity()
        {
            TEntity entity = new TEntity();
            entity.InjectFrom(this);
            return entity;
        }

        public virtual bool IsValid()
        {
            return true;
        }
    }



    public static class CollectionExtensions
    {
        public static ICollection<TTo> InjectFrom<TFrom,TTo>(this ICollection<TTo> to, IEnumerable<TFrom> from) where TTo : new()
        {
            foreach (var source in from)
            {
                var target = new TTo();
                target.InjectFrom(source);
                to.Add(target);
            }
            return to;
        }
    }
}
